
Page({
  data: {
    dynamicList: [], // 动态列表
    pageMsg: {
      pageNum: 1,
      isLastPage: false
    }, // 翻页信息
    needCheck: false, // 是否执行checkData
  },
  onLoad(t) {
    if (wx.canIUse('hideHomeButton') && wx.hideHomeButton) { wx.hideHomeButton() } 

    let params = {
      pageNum: 1,
	    pageSize: 10
    }
    if (getApp().globalData.userInfo && getApp().globalData.userInfo.memberId) {
      params.memberId = Number(getApp().globalData.userInfo.memberId)
    } else {
      this.setData({ needCheck: true })
    }
    this.getdynamicList(params, 'new')

  },
  onReady() {},
  onShow() {
    if (this.data.needCheck && getApp().globalData.userInfo && getApp().globalData.userInfo.memberId) {
      this.checkData();
    }
  },
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {
    if (!this.data.pageMsg.isLastPage) {
      let params = {
        pageNum: this.data.pageMsg.pageNum + 1,
        pageSize: 10
      }
      if (getApp().globalData.userInfo && getApp().globalData.userInfo.memberId) {
        params.memberId = Number(getApp().globalData.userInfo.memberId)
      }
      this.getdynamicList(params, 'next');
    }
  },
  onShareAppMessage() {
    let shareTitle = '新丰泽房地产'
    let shareLink = `/page/dynamic/index`
    let shareImg = ''
    console.log('share Data ===> ', {
      shareTitle,
      shareLink,
      shareImg
    })
    return {
      title: shareTitle,
      path: shareLink,
      imageUrl: shareImg,
    }
  },
  toWebView(e) { // 跳转公众号网页
    let target = e.currentTarget.dataset;
    // if (target.url) {
    if (false) {
      let URI = encodeURIComponent(target.url);
      wx.navigateTo({ url: `/page/webview/webview?to=${URI}` });
    } else {
      wx.showToast({
        title: '该动态暂未设置相关URL', //提示的内容,
        icon: 'none', //图标,
        duration: 2000, //延迟时间,
        mask: false, //显示透明蒙层，防止触摸穿透,
        success: res => {}
      });
    }
  },
  checkData() { // 登录后数据处理（处理未登录进入该页面，登录后返回该页面数据不重新加载，处理用户是否点赞问题）
    let newItem = [...this.data.dynamicList];
    
    newItem.map((item, index) => {
      item.likeList.map((items, indexs) => {
        if (items.memberId == getApp().globalData.userInfo.memberId) {
          item.islike = '1'
        }
      })
    })
    this.setData({
      dynamicList: newItem,
      needCheck: false
    })
  },
  // ---------------------------------------------
  getdynamicList(params, type) { // 获取列表
    getApp().api.getDynamicList(params).then(res => {
      if (res.data.list.length > 0) {
        res.data.list.map(item => {
          item._createTime = item.createTime.slice(0, 19)
        })
      }
      this.setData({
        dynamicList: type === 'new' ? res.data.list || [] : [...this.data.dynamicList, ...(res.data.list || [])],
        pageMsg: {
          pageNum: res.data.pageNum,
          isLastPage: res.data.isLastPage
        }
      })
    })
  },
  putLike(e) { // 点赞&取消
    let target = e.currentTarget.dataset;
    if (!getApp().globalData.userInfo || !getApp().globalData.userInfo.memberId) {
      // this.needLogin({type: 'switchTab'});
      this.needLogin(target.index);
      return false;
    }
    let params = {
      dynamicId: String(this.data.dynamicList[target.index].id),
      memberId: String(getApp().globalData.userInfo.memberId),
      type: this.data.dynamicList[target.index].islike == 1 ? '0':'1'
    }
    getApp().api.putLike(params).then(res => {
      if (res.code == 200) {
        let newItem = {...this.data.dynamicList[target.index]}, uIndex = -1;
        newItem.islike = params.type;

        newItem.likeList.map((item, index) => {
          if (item.memberId == getApp().globalData.userInfo.memberId) {
            uIndex = index;
          }
        })
        if (params.type == 1 && uIndex == -1) {
          newItem.likeList.push({memberId: getApp().globalData.userInfo.memberId, createName: getApp().globalData.userInfo.nickName})
        } else {
          newItem.likeList.splice(uIndex, 1)
        }
        this.data.dynamicList.splice(target.index, 1, newItem);
        this.setData({
          dynamicList: this.data.dynamicList
        })
      }
    })
  },
  needLogin(target) {
    this.data.dynamicList[target].islike = this.data.dynamicList[target].islike === 1 ? 0 : 1
    this.setData({
      dynamicList: this.data.dynamicList
    })
  }
})